//
//  BK_OD_Footer_View.m
//  bestkeep
//
//  Created by TOPTEAM on 2017/3/3.
//  Copyright © 2017年 utouu. All rights reserved.
//

#import "BK_OD_Footer_View.h"
#import "BK_OD_RedDot_View.h"
#import "BK_OD_Color.h"
#pragma mark======================BK_OD_Footer_View============================
/**
 加入购物车与立即支付
 */
@implementation BK_OD_Footer_View
{
    UIButton *_addShoppingCar;
    UIButton *_payment;
    UIButton *_goShoppingCar;
    BK_OD_RedDot_View *_redDot;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initialize];
    }
    return self;
}

- (void)initialize
{
    self.layer.shadowOffset = CGSizeMake(0, -1);
    self.layer.shadowOpacity = 0.3;
    
    _addShoppingCar = [[UIButton alloc] init];
    [_addShoppingCar setTitle:@"加入购物车" forState:UIControlStateNormal];
    [_addShoppingCar setTitleColor:kOrderWhite forState:UIControlStateNormal];
    _addShoppingCar.titleLabel.font = UIFontBoldCustom(18);
    UIImage *scImage = [Common imageWithFrame:CGRectMake(0, 0, 1, 1) color:kOrderBKGreen2];
    [_addShoppingCar setBackgroundImage:scImage forState:UIControlStateNormal];
    [_addShoppingCar addTarget:self action:@selector(addBuyCarAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_addShoppingCar];
    
    _goShoppingCar = [[UIButton alloc] init];
    [_goShoppingCar setImage:[UIImage imageNamed:@"BKOrder_SC"] forState:UIControlStateNormal];
    _goShoppingCar.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [_goShoppingCar setImageEdgeInsets:UIEdgeInsetsMake(8, 8, 8, 8)];
    UIImage *gscImage = [Common imageWithFrame:CGRectMake(0, 0, 1, 1) color:kOrderWhite];
    [_goShoppingCar setBackgroundImage:gscImage forState:UIControlStateNormal];
    [_goShoppingCar addTarget:self action:@selector(goShoppingCarAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_goShoppingCar];
    
    _payment = [[UIButton alloc] init];
    [_payment setTitle:@"立即购买" forState:UIControlStateNormal];
    [_payment setTitleColor:kOrderWhite forState:UIControlStateNormal];
    _payment.titleLabel.font = UIFontBoldCustom(18);
    UIImage *pImage = [Common imageWithFrame:CGRectMake(0, 0, 1, 1) color:kOrderBKGreen];
    [_payment setBackgroundImage:pImage forState:UIControlStateNormal];
    [_payment addTarget:self action:@selector(paymentAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_payment];
    
    [self setCanSubmit:NO];
    
    _redDot = [[BK_OD_RedDot_View alloc] init];
    [self updateShoppingCartAmount];
    [self addSubview:_redDot];
    
    [self makeConstraints];
}

- (void)makeConstraints
{
    [_payment mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.mas_equalTo(self);
        make.left.mas_equalTo(_addShoppingCar.mas_right);
    }];
    
    
    [_goShoppingCar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_equalTo(self);
        make.width.mas_equalTo(_goShoppingCar.mas_height).multipliedBy(1.0);
    }];
    
    [_addShoppingCar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_goShoppingCar.mas_right);
        make.top.bottom.mas_equalTo(self);
        make.width.mas_equalTo(_payment.mas_width);
    }];
    
    [_redDot mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_goShoppingCar.mas_centerX);
        make.top.right.mas_equalTo(_goShoppingCar);
        make.bottom.mas_equalTo(_goShoppingCar.mas_centerY);
    }];
}

- (void)goShoppingCarAction:(UIButton *)sender
{
    if (self.delegate && [self.delegate conformsToProtocol:@protocol(BKODBFooterViewDelegate)]) {
        [self.delegate ODBFooterViewGoShoppingCar];
    }
}

- (void)addBuyCarAction:(UIButton *)sender
{
    if (self.delegate && [self.delegate conformsToProtocol:@protocol(BKODBFooterViewDelegate)]) {
        [self.delegate ODBFooterViewAddShoppingCar];
    }
}

- (void)paymentAction:(UIButton *)sender
{
    if (self.delegate && [self.delegate conformsToProtocol:@protocol(BKODBFooterViewDelegate)]) {
        [self.delegate ODBFooterViewPayment];
    }
}

- (void)setCanSubmit:(BOOL)canSubmit
{
    if (canSubmit) {
        _addShoppingCar.userInteractionEnabled = YES;
        _payment.userInteractionEnabled = YES;
//        _addShoppingCar.enabled = YES;
//        _payment.enabled = YES;
    }else{
        _addShoppingCar.userInteractionEnabled = NO;
        _payment.userInteractionEnabled = NO;
//        _addShoppingCar.enabled = NO;
//        _payment.enabled = NO;
    }
}

- (void)updateShoppingCartAmount
{
    [[Mediator sharedInstance] BKSC_getScAmount:^(NSString *amount) {
        if ([amount integerValue] <= 0) {
            _redDot.hidden = YES;
        }else{
            _redDot.hidden = NO;
            [_redDot setCount:[amount integerValue]];
        }
    }];
}
@end












































